(function(){
    function Project(){
        var breadcrumb = new App.models.Breadcrumb({
            prefix: [
                {text: '进程管理', anchor: '/task'}
            ],
            curr: '业务工程'
        });

        var ProjectTableOpts = {
            el: '#project-wrapper .process-list',
            columns: [
                {field: 'name', title: '名称'},
                {field: 'path', title: '路径'},
                {field: 'psStr', title: 'ps cmd'},
                {field: 'owner', title: '属主'},
                {field: 'user', title: '启动用户'},
                {field: 'pids', title: 'pids'},
                {field: 'ports', title: '占用端口'},
                {
                    field: 'startDate', 
                    title: '启动日期',
                    formatter: function(value, row, index){
                        // 项目进程不一定正在运行, 需要区分
                        if(!value) return '';
                        _list = value.replace(/Z/g, "").split('T');

                        var text = _list[0];
                        var title = _list.join(' ');
                        return textTooltip(title, text);
                    }
                },
                {field: 'status', title: '运行状态'},
                {field: 'remark', title: '备注'},
                {
                    field: 'operation', 
                    title: '操作',
                    class: 'operate-col',
                    formatter: function(value, row, index){
                        return [
                            '<span class="btn-edit-inline"><i class="fa fa-pencil-square-o"></i></span>',
                            '<span class="btn-del-inline"><i class="fa fa-trash-o"></i></span>',
                        ].join('');
                    }
                },
            ],
            opts:  {
                url: App.apiList.project + '/query?minionId=' + App.xbox.minionId,                
                toolbar: '#project-wrapper .toolbar',
            }
        };
        var ProjectTable = App.views.BaseTableView.extend({});

        var MainView = Backbone.View.extend({
            // el与initialize一样是bk的关键字, 不能重命名为其它
            el: '#page-cnt',
            template:  _.template($("#tpl-project").html()),
            events: {
                'click .btn-create': 'showProjectModal'
            },
            initialize: function() {
                // backbone的事件总线机制, 子视图之间可以由总线上报到主视图再与其他子视图通信
                this.eventBus = _.extend({}, Backbone.Events);
                this.breadcrumbView = new App.views.BreadcrumbView({model: breadcrumb});
                this.$el.html(this.template());
                this.procInfoModel = new App.models.ProjectInfo();

                // 表格组件
                this.projTable = new ProjectTable(Object.assign( 
                    {
                        eventBus: this.eventBus,
                        dataModel: this.procInfoModel
                    },
                    ProjectTableOpts, 
                ));
                // 工程注册框组件
                this.registerView = new App.views.RegisterView({
                    eventBus: this.eventBus,
                    model: this.procInfoModel
                });
                this.watch();
            },
            watch: function(){
                var _self = this;
                this.eventBus.on('requestSuccess', function(){
                    _self.projTable.$el.bootstrapTable('refresh');
                    _self.registerView.hide();
                });
            },
            showProjectModal: function(){
                var procInfo = {
                    minionId: '',
                    name: '',
                    user: '',
                    psStr: '',
                    ports: '',
                    path: '',
                    startCmd: '',
                    remark: '',
                };
                this.registerView.render(procInfo);
            }
        });

        var mainView = new MainView();
    }
    App.modules.Project = Project;
})();
